<template>
  <div class="app-container">
    <div style="margin-bottom: 55px;">
      <el-row>
        <el-card shadow="never">
          <el-row style="display: flex; align-items: center;">
            <el-col :span="2">
              <el-button style="fpadding: 3px 0" type="text" icon="el-icon-arrow-left" size="small">返回</el-button>
            </el-col>
            <el-col :span="18">
                  <el-tag type="primary" effect="dark" style="margin-right: 20px">结算单</el-tag>
                  <strong>{{detailForm.settleId}}</strong>
                  <el-divider direction="vertical"></el-divider>
                  <span>时间：{{detailForm.settleTime}}</span>
                  <el-divider direction="vertical"></el-divider>
                  <span>单据状态: </span>
                  <el-link type="success" :underline="false"><i class="el-icon-circle-check"></i>{{detailForm.settlebillStatusName}}</el-link>
                  <el-divider direction="vertical"></el-divider>
                  <span>单据性质: </span>
                  <el-link type="success" :underline="false">{{detailForm.natureTypeName}}</el-link>
                  <el-divider direction="vertical"></el-divider>
            </el-col>
            <el-col :span="6">
              <el-button type="primary" style="float: right;margin-left: 10px" size="small" @click="handleOpenInvoice" disabled="disabled">保存</el-button>
            </el-col>
          </el-row>
        </el-card>
      </el-row>
      <el-row style="padding-top: 10px" class="v-deep">
        <el-col :span="24" :xs="24">
          <el-collapse v-model="activeNames2">
            <el-collapse-item name="1">
              <template slot="title">
                <div>主体信息</div>
              </template>
              <div style="padding: 5px">
                <el-form ref="detailForm" v-model="detailForm" label-position="top" label-width="80px" size="small">
                  <el-row :gutter="10">
                    <el-col :span="6">
                      <el-form-item label="发票类型" prop="invoiceType">
                        <el-select v-model="detailForm.invoiceType">
                          <el-option
                            v-for="dict in dict.type.invoice_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="特定业务" prop="businessType">
                        <el-select v-model="detailForm.businessType">
                          <el-option
                            v-for="dict in dict.type.business_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="parseInt(dict.value)"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="征收类型"  prop="collectionType">
                        <el-select v-model="detailForm.collectionType">
                          <el-option
                            v-for="dict in dict.type.collection_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="parseInt(dict.value)"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="订单号"  prop="orderNo">
                        <el-input v-model="detailForm.orderNo"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="6">
                      <el-form-item label="购方名称" prop="buyerName">
                        <el-input v-model="detailForm.buyerName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="购方纳税识别号" prop="buyerNumber">
                        <el-input v-model="detailForm.buyerNumber"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="购方地址">
                        <el-input v-model="detailForm.buyerAddress"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="购方电话">
                        <el-input v-model="detailForm.buyerTel"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="6">
                      <el-form-item label="购方开户行">
                        <el-input v-model="detailForm.buyerBank"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="购方账号">
                        <el-input v-model="detailForm.buyerAccount"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="6">
                      <el-form-item label="销方名称">
                        <el-input v-model="detailForm.sellerName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="销方纳税识别号">
                        <el-input v-model="detailForm.sellerNumber"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="销方地址">
                        <el-input v-model="detailForm.sellerAddress"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="销方电话">
                        <el-input v-model="detailForm.sellerTel"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="销方开户行">
                        <el-input v-model="detailForm.sellerBank"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="销方账号">
                        <el-input v-model="detailForm.sellerAccount"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="6">
                      <el-form-item label="开票人">
                        <el-input v-model="detailForm.invoiceName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="收款人">
                        <el-input v-model="detailForm.collectName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="收票人邮箱">
                        <el-input v-model="detailForm.collectMail"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="收票人手机号">
                        <el-input v-model="detailForm.collectMobile"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">

                    <el-col :span="6">
                      <el-form-item label="发货日期" prop="deliveryTime">
                        <el-date-picker clearable
                                        style="width: 100%;"
                                        v-model="detailForm.deliveryTime"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        placeholder="请选择发货日期">
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="结算日期" prop="settleTime">
                        <el-date-picker clearable
                                        style="width: 100%;"
                                        v-model="detailForm.settleTime"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        placeholder="请选择结算日期">
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="特殊开票">
                        <el-input v-model="detailForm.voucherNumber"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="开票网点">
                        <el-input v-model="detailForm.specialInvoice"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="站点">
                        <el-input v-model="detailForm.branchName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="备注">
                        <el-input v-model="detailForm.remark"></el-input>
                      </el-form-item>
                    </el-col>

                  </el-row>
                </el-form>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-col>
      </el-row>
      <el-row style="padding-top: 10px" class="v-deep">
        <el-col :span="24" :xs="24">
          <el-collapse v-model="activeNames">
            <el-collapse-item name="1" >
              <template slot="title">
                <div>商品信息</div>
              </template>
              <div style="padding: 5px" class="low-height-th-table">
                <el-table
                  border
                  stripe
                  default-expand-all
                  ref="tableRef"
                  v-loading="loading"
                  :data="settlebillEntryList"
                  :header-cell-style= "{
                    'font-size': '12px !important',
                    'font-weight': '500 !important',
                    'color':'#FFF',
                    'padding': '0px 0',
                    'background-color':'rgb(51 112 255 / 80%)',
                    textAlign: 'left',
                    'height': '25px !important'
                  }"
                  :cell-style="{ padding: '8px 10px 8px 0', textAlign: 'left', 'height': '25px !important' }"
                  highlight-current-row style="width: 100%;"
                  height="320px"
                >
                  <el-table-column type="selection" width="55" align="center" />
                  <el-table-column label="序号" align="center" prop="index" width="50">
                    <template slot-scope="scope">{{scope.$index+1}}</template>
                  </el-table-column>
                  <el-table-column label="产品名称" align="left" prop="productCode" width="250">
                    <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.productName" maxlength = 0 clearable placeholder="请选择"  readonly>
                        <el-button slot="append" icon="el-icon-search" style="padding: 0 10px;" @click="openSearchProductDialogOpen(scope.$index)">
                        </el-button>
                      </el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="税编简称" align="left" prop="taxShortName" width="180">
                    <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.taxShortName" maxlength = 0 clearable placeholder="请选择" readonly>
                        <el-button slot="append" icon="el-icon-search" style="padding: 0 10px;" @click="openSearchTaxCategoryDialogOpen(scope.$index)">
                        </el-button>
                      </el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="规格型号" align="left" prop="specificatCode" width="150">
                    <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.specificatCode" clearable placeholder="请输入" >
                      </el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="单位" align="left" prop="unit" width="120">
                    <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.unit" clearable placeholder="请输入" >
                      </el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="数量" align="left" prop="qty" width="140">
                    <template slot-scope="scope">
                      <el-input-number size="mini" v-model="scope.row.qty" controls-position="right" :min="1" :max="9999999" @change="changePrice(scope.$index,scope.row)" style="width: 100px;"></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="单价(不含税)" align="left" prop="withoutPrice"  width="160" >
                    <template slot-scope="scope">
                      <el-input-number size="mini" v-model="scope.row.withoutPrice" :precision="8" @change="changePrice(scope.$index,scope.row)"controls-position="right" :min="1" :max="9999999" style="width: 100px;"></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="金额(不含税)" align="left" prop="withoutAmount" width="140"  >
                    <template slot-scope="scope">
                      {{scope.row.withoutAmount}}
                    </template>
                  </el-table-column>

                  <el-table-column label="单价(含税)" align="left" prop="price" width="160" >
                    <template slot-scope="scope">
                      <el-input-number size="mini" v-model="scope.row.price" :precision="8"  @change="changePrice(scope.$index,scope.row)" controls-position="right" :min="1" :max="9999999" style="width: 100px;"></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="金额(含税)" align="left" prop="amount" width="140">
                    <template slot-scope="scope">
                      {{scope.row.amount}}
                    </template>
                  </el-table-column>

                  <el-table-column label="折扣金额(不含税)" align="left" prop="withoutDiscountAmount" width="160"  >
                    <template slot-scope="scope">
                      <el-input-number size="mini" v-model="scope.row.withoutDiscountAmount" :precision="8" controls-position="right" :min="1" :max="9999999" style="width: 100px;"></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="折扣金额(含税)" align="left" prop="discountAmount" width="160">
                    <template slot-scope="scope">
                      <el-input-number size="mini" v-model="scope.row.discountAmount" :precision="8" controls-position="right" :min="1" :max="9999999" style="width: 100px;"></el-input-number>
                    </template>
                  </el-table-column>

                  <el-table-column label="税率" align="left" prop="taxRate"  width="140">
                    <template slot-scope="scope">
                      <el-select size="mini" clearable v-model="scope.row.taxRate" style="width:100%" @change="changeTax(scope.$index,scope.row)">
                        <el-option v-for="dict in dict.type.tax_rate" :key="dict.label" :label="dict.label"
                                   :value="dict.value"></el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="税额" align="left" prop="tax"  width="120">
                    <template slot-scope="scope">
                      {{scope.row.tax | formatCurrency}}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120" >
                    <template slot-scope="scope">
                      <el-button
                        type="text"
                        size="mini"
                        @click="deleteEntry(scope.$index)"
                        style="color: red"
                        v-if="scope.$index > 0"
                      >移除</el-button>
                      <el-divider direction="vertical" v-if="scope.$index > 0"></el-divider>
                      <el-button
                        type="text"
                        size="mini"
                        @click="addEntry()"
                      >新增</el-button>
                    </template>
                  </el-table-column>
                  <div slot="empty">
                    <svg-icon icon-class="search-none" style="font-size: 64px;" />
                    <p>暂无数据</p>
                  </div>
                </el-table>
<!--                <div>-->
<!--                  <div style="margin-top: 5px;margin-bottom: 5px;float: right;">-->
<!--                    <el-radio-group v-model="isWithout" size="mini">-->
<!--                      <el-radio-button label="含税"></el-radio-button>-->
<!--                      <el-radio-button label="不含税"></el-radio-button>-->
<!--                    </el-radio-group>-->
<!--                  </div>-->
<!--                </div>-->
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-col>
      </el-row>
    </div>
    <div style="position: fixed;z-index: 999;bottom: 0px;width: 100%;margin-top: 16px;box-shadow: rgba(0, 0, 0, 0.15) 0px 2px 12px 4px;">
      <div style="display: flex;height: 56px;background-color: #FFFF;">
        <div style="width: 100px;padding: 15px 28px;background-color: #3370ff;color: #FFF;font-size: 20px;font-weight: 600;">
          <span>合计</span>
        </div>
        <div style="display: flex;flex-grow: 1;align-items: center;">
          <div style="width: 350px;padding: 0 15px;">
            <svg-icon icon-class="without-tax2" style="color: rgb(255, 88, 88);    font-size: 18px;"/> 金额(不含税)
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.feeWithoutTax | formatCurrency}}
            </span>

          </div>
          <div style="width: 350px;padding: 0 15px;">
            <svg-icon icon-class="with-tax2" style="color: rgb(255, 165, 71);    font-size: 18px;"/>  金额(含税)
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.fee | formatCurrency}}
            </span>

          </div>
          <div style="width: 300px;padding: 0 15px;">
            <svg-icon icon-class="tax"style="color: rgb(41, 180, 240);    font-size: 18px;" />  税额
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.tax | formatCurrency}}
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 查看发票 -->
    <el-dialog v-dialogDrag  :close-on-click-modal="false" title="查看发票" :visible.sync="invoiceOpen" width="1400px" append-to-body>
      <div class="dialog_box" style="height:650px;">
        <el-row>
            <el-col :span="10">
              <div style="height:575px; overflow-y: auto">
                <el-tabs type="border-card">
                <el-tab-pane label="发票信息">
                  <el-descriptions class="margin-top" title="基本信息" :column="2" size="mini" border>
                    <template slot="title">
                      <div>基本信息 <span class="invoice-type">{{detailForm.invoiceTypeName}}</span></div>
                    </template>
                    <el-descriptions-item>
                      <template slot="label">
                        发票代码：
                      </template>
                      {{detailForm.invoiceCode}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        发票号码：
                      </template>
                      {{detailForm.invoiceNo}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        开票日期：
                      </template>
                      {{detailForm.invoiceDate}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        金额(不含税):
                      </template>
                      ¥{{detailForm.feeWithoutTax | formatCurrency}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        税额:
                      </template>
                      ¥{{detailForm.tax | formatCurrency}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        金额(含税):
                      </template>
                      ¥{{detailForm.fee | formatCurrency}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        校验码:
                      </template>
                      {{detailForm.checkCode}}
                    </el-descriptions-item>
                  </el-descriptions>

                  <el-descriptions class="margin-top" title="抬头信息" :column="1" size="mini" border>
                    <template slot="extra">
                      <el-radio-group v-model="isWithout" size="mini">
                        <el-radio-button label="销方"></el-radio-button>
                        <el-radio-button label="购方"></el-radio-button>
                      </el-radio-group>
                    </template>
                    <el-descriptions-item>
                      <template slot="label">
                        名称:
                      </template>
                      {{detailForm.name}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        纳税识别号:
                      </template>
                      {{detailForm.buyerNumber}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        地址、电话:
                      </template>
                      {{detailForm.buyerAddress}}-{{detailForm.buyerTel}}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        开户行及账号:
                      </template>
                      {{detailForm.buyerBank}}-{{detailForm.buyerAccount}}
                    </el-descriptions-item>
                  </el-descriptions>

                <el-descriptions class="margin-top" title="业务信息" :column="2" size="mini" border>

                  <el-descriptions-item>
                    <template slot="label">
                      <i class="el-icon-user"></i>
                      发货日期:
                    </template>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template slot="label">
                      特殊开票:
                    </template>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template slot="label">
                      开票网点:
                    </template>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template slot="label">
                      站点:
                    </template>
                  </el-descriptions-item>
                </el-descriptions>
                <el-descriptions class="margin-top" title="商品明细" :column="1" size="mini" border >
                    <template slot="extra">
                      <el-radio-group v-model="isWithout" size="mini">
                        <el-radio-button label="含税"></el-radio-button>
                        <el-radio-button label="不含税"></el-radio-button>
                      </el-radio-group>
                    </template>
                    <el-descriptions-item>
                      <el-table
                        border
                        stripe
                        default-expand-all
                        ref="invoiceDetailEntry"
                        v-loading="loading"
                        :data="settlebillEntryList"
                        :header-cell-style= "{
                          'font-size': '12px !important',
                          'font-weight': '500 !important',
                          'color':'#FFF',
                          'padding': '0px 0',
                          'background-color':'rgb(51 112 255 / 80%)',
                          textAlign: 'left',
                          'height': '30px !important'
                        }"
                        :cell-style="{ padding: '8px 10px 8px 0', textAlign: 'left' }"
                        highlight-current-row
                        style="width: 555px;margin-left: -32px;margin-top: -10px;"
                        height="300px"
                      >
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="序号" align="center" prop="index" width="50">
                          <template slot-scope="scope">{{scope.$index+1}}</template>
                        </el-table-column>
                        <el-table-column label="产品名称" align="left" prop="productName" width="250">
                        </el-table-column>
                        <el-table-column label="税编简称" align="left" prop="taxShortName" width="180">
                        </el-table-column>
                        <el-table-column label="规格型号" align="left" prop="specificatCode" width="150">
                        </el-table-column>
                        <el-table-column label="单位" align="left" prop="unit" width="120">
                        </el-table-column>
                        <el-table-column label="数量" align="left" prop="qty" width="140">
                        </el-table-column>
                        <el-table-column label="单价(不含税)" align="left" prop="withoutPrice"  width="160" >
                        </el-table-column>
                        <el-table-column label="金额(不含税)" align="left" prop="withoutAmount" width="140"  >
                        </el-table-column>
                        <el-table-column label="单价(含税)" align="left" prop="price" width="160" >
                        </el-table-column>
                        <el-table-column label="金额(含税)" align="left" prop="amount" width="140">
                        </el-table-column>
                        <el-table-column label="折扣金额(不含税)" align="left" prop="withoutDiscountAmount" width="160"  >
                        </el-table-column>
                        <el-table-column label="折扣金额(含税)" align="left" prop="discountAmount" width="160">
                        </el-table-column>
                        <el-table-column label="税率" align="left" prop="taxRate"  width="140">
                        </el-table-column>
                        <el-table-column label="税额" align="left" prop="tax"  width="120">
                        </el-table-column>
                        <div slot="empty">
                          <svg-icon icon-class="search-none" style="font-size: 64px;" />
                          <p>暂无数据</p>
                        </div>
                      </el-table>
                    </el-descriptions-item>

                  </el-descriptions>


                </el-tab-pane>
              </el-tabs>
              </div>
            </el-col>
        </el-row>
      </div>
    </el-dialog>


  </div>

</template>
<style lang="scss">
  .input_num .el-input__inner{
    padding: 0 0 0 15px !important;
    color: red;
    font-weight: 700;
  }
  .el-table .warning-row {
    background: oldlace;
  }
  .el-table .success-row {
    background: #f0f9eb;
  }
  .el-card__body, .el-main {
    padding: 5px 20px !important;
  }

  .collapse-title{
    background-color: red;
  }
  .el-collapse-item__header.is-active {
    border-bottom: 1px solid #f0f2f5 !important;
  }
  .el-collapse-item__header::before{
    content: "";
    display: block;
    width: 3px;
    height: 20px;
    position: absolute;
    background-color: #297cf0;
    left: 0px;
    top: 35px;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
  }

  .el-collapse-item__header div{
    margin-left: 10px;
  }
  .el-form-item__label{
    margin-bottom: -12px;
  }

  .v-deep{
    .el-collapse-item__arrow {
      width: 40px;
    }
    .el-icon-arrow-right:before {
      content: "展开";
      font-size: 14px;
      font-family: 'heiti';
      color: #2295ff;
    }
    .el-collapse-item__arrow.is-active {
      transform: none;
    }
    .el-collapse-item__arrow.is-active::before {
      content: "收起";
      font-size: 14px;
      font-family: 'heiti';
      color: #2295ff;
    }
    .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 8px;
    }
  }

  .el-descriptions__header{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin-bottom: 5px !important;
    margin-top: 5px;
  }
  .el-descriptions__title::before{
    content: "";
    display: block;
    width: 3px;
    height: 20px;
    position: absolute;
    background-color: #297cf0;
    left: 0px;
    margin-top: 11px;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
  }
  .invoice-type {
    font-size: 12px;
    font-weight: 500;
    border: 2px solid;
    padding: 2px 6px;
    color: hsl(158deg 86% 34%);
    border-radius: 4px;
  }
  .invoice-type::before,
  .invoice-type::after {
    position: absolute;
    content: '';
    width: 6px;
    height: 6px;
    background-color: #fff;
    border: 2px solid hsl(158deg 86% 34%);
    border-left-color: transparent;
    border-bottom-color: transparent;
    border-radius: 50%;
    margin-top: 10px;
  }

  .invoice-type::before {
    margin-left: -10px;
    transform: rotate(45deg);
  }

  .invoice-type::after {
    margin-left: 3px;
    transform: rotate(-135deg);
  }

</style>

<script>
import { listSettlebill, getSettlebill, delSettlebill, addSettlebill, updateSettlebill } from "@/api/doc/settlebill";
import Viewer from 'v-viewer';
export default {
    name: "SettleBillUpdate",
    dicts: ['invoice_type', 'collection_type', 'business_type', 'tax_rate'],
    data() {
        return {
            activeNames: ['1'],
            activeNames2: ['1'],
            detailForm:{},
            loading:false,
            settlebillEntryList:[],
            isWithout:'含税',
            invoiceOpen:false,
            options:{
                zIndex: 99999,
                inline: true,
                button: true,
                navbar: true,
                title: true,
                toolbar: true,
                tooltip: true,
                movable: true,
                zoomable: true,
                rotatable: true,
                scalable: true,
                transition: true,
                fullscreen: false,
                keyboard: true,
                url: 'data-source'
            },

            currentRowIdx : 0,
            /* ======================================查询条件-产品选择======================================  */
            searchProductCodes:[],
            searchProductNames:[],
            searchProductCodeTotal: 0,
            searchProductCodeList: [],
            searchProductCodeDialogOpen: false,
            searchProductCodeSelectLoading: false,
            searchProductCodeQueryParams: {
                searchValue: null,
                pageNum: 1,
                pageSize: 20,
            },

            /* ======================================查询条件-产品选择======================================  */
            searchTaxCategoryCodes:[],
            searchTaxCategoryNames:[],
            searchTaxCategoryCodeTotal: 0,
            searchTaxCategoryCodeList: [],
            searchTaxCategoryCodeDialogOpen: false,
            searchTaxCategoryCodeSelectLoading: false,
            searchTaxCategoryCodeQueryParams: {
                searchValue: null,
                pageNum: 1,
                pageSize: 20,
            },
        };
    },
    created() {
        // 获取发票ID
        let settleId = this.$route.params && this.$route.query.settleId;
        console.log("获取结算单ID：" + settleId);
        this.init(settleId);
    },
    methods: {
        init(settleId){
            this.$modal.loading("正在初始化，请稍候...");
            getSettlebill(settleId).then(response => {
                this.detailForm = response.data;
                this.settlebillEntryList = response.data.tscSettlebillEntryList;
                this.$modal.closeLoading();
            });
        },

        handleOpenInvoice() {
            this.invoiceOpen = true;
            // TODO 获取附件

        },

        nextInvoice(settleId){
            this.$modal.loading("正在初始化，请稍候...");
            getNextInvoice(settleId).then(response => {
                if(response.data){
                    this.detailForm = response.data;
                    this.settlebillEntryList = response.data.tscSettlebillEntryList;
                } else {
                    this.$modal.msgWarning("没有下一个票据");
                }
                this.$modal.closeLoading();
            });
        },

        preInvoice(settleId){
            this.$modal.loading("正在初始化，请稍候...");
            getPreInvoice(settleId).then(response => {
                if(response.data){
                    this.detailForm = response.data;
                    this.settlebillEntryList = response.data.tscSettlebillEntryList;
                } else {
                    this.$modal.msgWarning("没有上一个票据");
                }
                this.$modal.closeLoading();
            });
        },



        deleteEntry(idx){
            this.settlebillEntryList.splice(idx, 1);
            // 行号重新编排
            for(let i=0 ; i<this.settlebillEntryList.length ; i++){
                this.settlebillEntryList[i].lineNo = i+1;
            }
            this.form.rowCount = this.settlebillEntryList.length;
        },

        addEntry(){
            const item = {
                id : null,
                entryId : null,
                settleId : null,
                custId : null,
                siteCode : null,
                siteName : null,
                unit : '',
                qty : 0,
                withoutPrice : 0,
                withoutAmount : 0,
                taxRate : 0,
                tax : 0,
                deductAmount : 0,
                deductRate : 0,
                withoutDiscountAmount : 0,
                discountAmount : 0,
                price : 0,
                amount : 0,
                policyCode : null,
                policyName : null,
                freeTaxType : null,
                freeTaxTypeName : null,
                taxShortName : null,
                productCode : '',
                productName : '',
                specificatCode : null,
                createTime: null
            };
            this.settlebillEntryList.push(item);
        },

        /* ※※ 查询 产品 相关方法 start ※※ */
        /** ① 弹出查询条件-产品 */
        openSearchProductDialogOpen(idx){
            this.currentRowIdx = idx;
            this.searchProductCodeDialogOpen = true;
            // 默认查询
            this.handleSearchProductCodeQuery();
        },
        /** ② 单击行 **/
        handleSearchProductCodeRowClick(row, column, event){
            this.$refs.searchProductCodeSingleTable.toggleRowSelection(row);
        },
        /** ③ 行KEY **/
        getSearchProductCodeListRowKey(row) {
            return row.productCode
        },
        /** ④ 选中产品 **/
        handleSearchProductCodeSelectionChange(selection) {
            if(selection){
                if (selection.length > 1) {
                    const item = selection.pop();
                    this.$refs.searchProductCodeSingleTable.clearSelection();
                    this.$refs.searchProductCodeSingleTable.toggleRowSelection(item);
                    this.searchProductCodes = item.productCode;
                    this.searchProductNames = item.productName;
                } else {
                    if(selection[0]){
                        this.searchProductCodes = selection[0].productCode;
                        this.searchProductNames = selection[0].productName;
                    }
                }
            } else {
                this.$refs.table.clearSelection();
                this.searchProductCodes = null;
                this.searchProductNames = null;
            }
        },
        /** ⑤ 点击查询按钮 **/
        handleSearchProductCodeQuery(){
            this.searchProductCodeQueryParams.pageNum = 1;
            this.getSearchProductCodeList();
        },
        /** ⑥ 查询 **/
        getSearchProductCodeList(){
            this.searchProductCodeSelectLoading = true;
            listProduct(this.searchProductCodeQueryParams).then(response => {
                this.searchProductCodeList = response.rows;
                this.searchProductCodeTotal = response.total;
                this.searchProductCodeSelectLoading = false;
            });
        },
        /** ⑦ 点击确认查询 **/
        confirmSearchProductCode() {
            if(this.searchProductCodes){
                this.settlebillEntryList[this.currentRowIdx].productName = this.searchProductNames;
                this.settlebillEntryList[this.currentRowIdx].productCode = this.searchProductCodes;
            }
            // 关闭查询弹出框
            this.searchProductCodeCancel();
        },
        /** ⑧ 点击关闭查询弹出框 **/
        searchProductCodeCancel() {
            this.searchProductCodeDialogOpen = false;
        },
        /* ※※ 查询 产品 相关方法 end ※※ */



        /* ※※ 查询 税收分类 相关方法 start ※※ */
        /** ① 弹出查询条件-公司代码 */
        openSearchTaxCategoryDialogOpen(idx){
            this.currentRowIdx = idx;
            this.searchTaxCategoryCodeDialogOpen = true;
            // 默认查询
            this.handleSearchTaxCategoryCodeQuery();
        },
        /** ② 单击行 **/
        handleSearchTaxCategoryCodeRowClick(row, column, event){
            this.$refs.searchTaxCategoryCodeSingleTable.toggleRowSelection(row);
        },
        /** ③ 行KEY **/
        getSearchTaxCategoryCodeListRowKey(row) {
            return row.taxCategoryCode
        },
        /** ④ 选中税收分类 **/
        handleSearchTaxCategoryCodeSelectionChange(selection) {
            if(selection){
                if (selection.length > 1) {
                    const item = selection.pop();
                    this.$refs.searchTaxCategoryCodeSingleTable.clearSelection();
                    this.$refs.searchTaxCategoryCodeSingleTable.toggleRowSelection(item);
                    this.searchTaxCategoryCodes = item.taxCategoryCode;
                    this.searchTaxCategoryNames = item.taxShortName;
                } else {
                    if(selection[0]){
                        this.searchTaxCategoryCodes = selection[0].taxCategoryCode;
                        this.searchTaxCategoryNames = selection[0].taxShortName;
                    }
                }
            } else {
                this.$refs.table.clearSelection();
                this.searchTaxCategoryCodes = null;
                this.searchTaxCategoryNames = null;
            }
        },
        /** ⑤ 点击查询按钮 **/
        handleSearchTaxCategoryCodeQuery(){
            this.searchTaxCategoryCodeQueryParams.pageNum = 1;
            this.getSearchTaxCategoryCodeList();
        },
        /** ⑥ 查询 **/
        getSearchTaxCategoryCodeList(){
            this.searchTaxCategoryCodeSelectLoading = true;
            listTaxcategory(this.searchTaxCategoryCodeQueryParams).then(response => {
                this.searchTaxCategoryCodeList = response.rows;
                this.searchTaxCategoryCodeTotal = response.total;
                this.searchTaxCategoryCodeSelectLoading = false;
            });
        },
        /** ⑦ 点击确认查询 **/
        confirmSearchTaxCategoryCode() {
            if(this.searchTaxCategoryCodes){
                this.settlebillEntryList[this.currentRowIdx].taxShortName = this.searchTaxCategoryNames;
                this.settlebillEntryList[this.currentRowIdx].taxCategoryCode = this.searchTaxCategoryCodes;
            }
            // 关闭查询弹出框
            this.searchTaxCategoryCodeCancel();
        },
        /** ⑧ 点击关闭查询弹出框 **/
        searchTaxCategoryCodeCancel() {
            this.searchTaxCategoryCodeDialogOpen = false;
        },
        /* ※※ 查询 公司代码 相关方法 end ※※ */

        // 计算金额
        changePrice(idx, row){
            row.withoutAmount = Number(row.withoutPrice * row.qty).toFixed(8);
            row.amount = Number(row.price * row.qty).toFixed(8);
            this.refreshSummary();
        },
        // 计算税额
        changeTax(idx, row){
            row.tax = Number(row.amount * row.taxRate * (0.01)).toFixed(8);
            this.refreshSummary();
        },

        refreshSummary(){
            let feeWithoutTax = 0;
            let fee = 0;
            let tax = 0;
            this.settlebillEntryList.forEach(item => {
                feeWithoutTax = Number(item.withoutAmount) + feeWithoutTax;
                fee = Number(item.amount) + fee;
                tax = Number(item.tax) + tax;
            });
            this.detailForm.feeWithoutTax = Number(feeWithoutTax).toFixed(8);
            this.detailForm.fee = Number(fee).toFixed(8);
            this.detailForm.tax = Number(tax).toFixed(8);
        }
    }
}
</script>
